home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 14 / RECYCLE.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  5KB  |  160 lines

  1. // File from page 625 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: RECYCLE.CPP -- Containers & polymorphism
  34. #include <fstream.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #include "..\14\tstack.h"
  38. ofstream out("recycle.out");
  39.  
  40. enum type { Aluminum, Paper, Glass };
  41.  
  42. class trash {
  43.   float Weight;
  44. public:
  45.   trash(float Wt) : Weight(Wt) {}
  46.   virtual type trashType() const = 0;
  47.   virtual const char* name() const = 0;
  48.   virtual float value() const = 0;
  49.   float weight() const { return Weight; }
  50.   virtual ~trash() {}
  51. };
  52.  
  53. class aluminum : public trash {
  54.   static float val;
  55. public:
  56.   aluminum(float Wt) : trash(Wt) {}
  57.   type trashType() const { return Aluminum; }
  58.   virtual const char* name() const {
  59.     return "aluminum";
  60.   }
  61.   float value() const { return val; }
  62.   static void value(int newval) {
  63.     val = newval;
  64.   }
  65. };
  66.  
  67. float aluminum::val = 1.67;
  68.  
  69. class paper : public trash {
  70.   static float val;
  71. public:
  72.   paper(float Wt) : trash(Wt) {}
  73.   type trashType() const { return Paper; }
  74.   virtual const char* name() const {
  75.     return "paper";
  76.   }
  77.   float value() const { return val; }
  78.   static void value(int newval) {
  79.     val = newval;
  80.   }
  81. };
  82.  
  83. float paper::val = 0.10;
  84.  
  85. class glass : public trash {
  86.   static float val;
  87. public:
  88.   glass(float Wt) : trash(Wt) {}
  89.   type trashType() const { return Glass; }
  90.   virtual const char* name() const {
  91.     return "glass";
  92.   }
  93.   float value() const { return val; }
  94.   static void value(int newval) {
  95.     val = newval;
  96.   }
  97. };
  98.  
  99. float glass::val = 0.23;
  100.  
  101. // Sums up the value of the trash in a bin:
  102. void SumValue(const tstack<trash>& bin,ostream& os){
  103.   tstackIterator<trash> tally(bin);
  104.   float val = 0;
  105.   while(tally) {
  106.     val += tally->weight() * tally->value();
  107.     os << "weight of " << tally->name()
  108.         << " = " << tally->weight() << endl;
  109.     tally++;
  110.   }
  111.   os << "Total value = " << val << endl;
  112. }
  113.  
  114. main() {
  115.   // Seed the random number generator
  116.   time_t t;
  117.   srand((unsigned)time(&t));
  118.  
  119.   tstack<trash> bin; // Default to ownership
  120.   // Fill up the trash bin:
  121.   for(int i = 0; i < 30; i++)
  122.     switch(rand() % 3) {
  123.       case 0 :
  124.         bin.push(new aluminum(rand() % 100));
  125.         break;
  126.       case 1 :
  127.         bin.push(new paper(rand() % 100));
  128.         break;
  129.       case 2 :
  130.         bin.push(new glass(rand() % 100));
  131.         break;
  132.     }
  133.   // Bins to sort into:
  134.   tstack<trash> glassbin(0); // No ownership
  135.   tstack<trash> paperbin(0);
  136.   tstack<trash> ALbin(0);
  137.   tstackIterator<trash> sorter(bin);
  138.   // Sort the trash:
  139.   // (RTTI offers a nicer solution)
  140.   while(sorter) {
  141.     // Smart pointer call:
  142.     switch(sorter->trashType()) {
  143.       case Aluminum:
  144.         ALbin.push(sorter.current());
  145.         break;
  146.       case Paper:
  147.         paperbin.push(sorter.current());
  148.         break;
  149.       case Glass:
  150.         glassbin.push(sorter.current());
  151.         break;
  152.     }
  153.     sorter++;
  154.   }
  155.   SumValue(ALbin, out);
  156.   SumValue(paperbin, out);
  157.   SumValue(glassbin, out);
  158.   SumValue(bin, out);
  159. }
  160.